package com.yhsoft.bill.service.impl;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yhsoft.bill.controller.po.BalancePo;
import com.yhsoft.bill.controller.po.CountMapPo;
import com.yhsoft.bill.controller.request.SysCzInVo;
import com.yhsoft.bill.controller.request.SysCzMxInVo;
import com.yhsoft.bill.controller.request.SysYckLogInVo;
import com.yhsoft.bill.controller.response.SalerYfzkOutVo;
import com.yhsoft.bill.controller.response.SysCzMxOutVo;
import com.yhsoft.bill.controller.response.SysXhLogOutVo;
import com.yhsoft.bill.entity.SysYfzkMx;
import com.yhsoft.bill.mapper.SysYfzkMxMapper;
import com.yhsoft.bill.mapper.SysYszkMapper;
import com.yhsoft.bill.service.ISysYfzkMxService;
import com.yhsoft.buyer.controller.response.VenderOrdersOutVo;
import com.yhsoft.buyer.entity.BuyerOrder;
import com.yhsoft.buyer.mapper.BuyerOrderMapper;
import com.yhsoft.common.service.FuSiLingApiService;
import com.yhsoft.common.service.FuSiLingFlimApiService;
import com.yhsoft.common.service.TokenService;;
import com.yhsoft.flim.controller.po.BalanceJsonData;
import com.yhsoft.flim.controller.po.JsonData;
import com.yhsoft.flim.controller.response.FlimPayOutVo;
import com.yhsoft.flim.entity.MovieSeat;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
;
import java.io.IOException;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 平台应付账款明细 服务实现类
 * </p>
 *
 * @author yyyyyyyh
 * @since 2023-11-03
 */
@Service
@Slf4j
public class SysYfzkMxServiceImpl extends ServiceImpl<SysYfzkMxMapper, SysYfzkMx> implements ISysYfzkMxService {

    @Resource
    private BuyerOrderMapper buyerOrderMapper;

    @Resource
    private SysYszkMapper sysYszkMapper;

    @Override
    public String cz(SysCzInVo inVo) {
        SysYfzkMx sysYfzkMx = new SysYfzkMx();
        BeanUtils.copyProperties(inVo, sysYfzkMx);
        sysYfzkMx.setId(null);
        sysYfzkMx.setPayBy(TokenService.getUserBytoken().getName());
        sysYfzkMx.setPayTime(LocalDateTime.now());
        this.getBaseMapper().insert(sysYfzkMx);
        return "success";
    }

    @Override
    public IPage<SysCzMxOutVo> getCzMx(SysCzMxInVo inVo, Long pageNo, Long pageSize) {
        QueryWrapper<SysCzMxOutVo> wrapper = new QueryWrapper<>();
        wrapper.eq("a.company_id", inVo.getCompanyId());

        // 查询条件
        wrapper.ge((inVo.getStartTime() != null), "a.pay_time", inVo.getStartTime());
        if (inVo.getEndTime() != null) {
            wrapper.le("a.pay_time", LocalDateTime.of(inVo.getEndTime(), LocalTime.MAX));
        }
        // 添加经手人和付款金额条件
        wrapper.like(StringUtils.hasLength(inVo.getPayBy()), "a.pay_by", inVo.getPayBy());
        wrapper.eq((inVo.getPayAmount() != null), "a.pay_amount", inVo.getPayAmount());

        wrapper.orderByDesc("a.create_time");

        Page<SysCzMxOutVo> page = new Page<>(pageNo, pageSize);
        IPage<SysCzMxOutVo> iPage = baseMapper.getYfzkList(page, wrapper);

        return iPage;
    }

    @Override
    public IPage<SysXhLogOutVo> getXhLog(SysYckLogInVo inVo, Integer companyId, Long pageNo, Long pageSize) {
        QueryWrapper<VenderOrdersOutVo> wrapper = new QueryWrapper<>();
        wrapper.eq("vendor_id", companyId);
        wrapper.eq("order_status", 0);
        // 查询条件
        wrapper.like(StringUtils.hasLength(inVo.getOrderNo()), "order_id", inVo.getOrderNo());
        wrapper.like(StringUtils.hasLength(inVo.getVenderOrderNo()), "order_id", inVo.getVenderOrderNo());
        wrapper.eq(StringUtils.hasLength(inVo.getGoodsType()), "goods_type", inVo.getGoodsType());
        wrapper.like(StringUtils.hasLength(inVo.getVenderOrderNo()), "order_id", inVo.getVenderOrderNo());
        wrapper.like(StringUtils.hasLength(inVo.getGoodsName()), "goods_name", inVo.getGoodsName());
        wrapper.like(StringUtils.hasLength(inVo.getCompanyName()), "company_name", inVo.getCompanyName());
        wrapper.like(StringUtils.hasLength(inVo.getCompanyZt()), "company_name", inVo.getCompanyZt());
        wrapper.eq(inVo.getTotlePrice() != null, "totlePrice", inVo.getTotlePrice());

        wrapper.ge((inVo.getStartTime() != null), "pay_time", inVo.getStartTime());
        if (inVo.getEndTime() != null) {
            wrapper.le("pay_time", LocalDateTime.of(inVo.getEndTime(), LocalTime.MAX));
        }

        Page<SysXhLogOutVo> page = new Page<>(pageNo, pageSize);
        IPage<SysXhLogOutVo> iPage = this.baseMapper.getListPageByConditions(page, wrapper);

        return iPage;
    }

    /**
     * 未受金额=使用的授权额度2
     * 累计供货金额=所有商品的总成本价1
     * 已付金额=总充值金额3
     * 未付金额=累计供货-已付金额
     */
    @Override
    public SalerYfzkOutVo getPtTj() throws IOException {
        SalerYfzkOutVo salerYfzkOutVo = new SalerYfzkOutVo();
        LambdaQueryWrapper<BuyerOrder> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(BuyerOrder::getOrderStatus, 0);
        wrapper.groupBy(BuyerOrder::getGoodsType);
        List<CountMapPo> map = buyerOrderMapper.getTjList(wrapper);
        Map<Integer, BigDecimal> mapCount = new HashMap<>();
        for (CountMapPo a : map) {
            mapCount.put(a.getGoodsType(), a.getCount());
        }

        /**
         * 应收账款统计
         * 未收金额=渠道商使用的授权额度
         */
        BigDecimal totalYsAmount = sysYszkMapper.getTotalYsAmount();
        salerYfzkOutVo.setTotalYsAmount(totalYsAmount);

        /**
         * 应付账款统计
         * 累计供货金额=所有供应商提供的所有商品总成本价
         * 已付金额=总充值的金额
         * 未付金额=累计供货-已付金额
         * *这里的供货仅计算当前平台中的供货商
         */
        BigDecimal totalGhAmount = map == null ? BigDecimal.ZERO : mapCount.getOrDefault(5, BigDecimal.ZERO);
        salerYfzkOutVo.setTotalGhAmount(totalGhAmount);

        BigDecimal totalPayAmount = sysYszkMapper.getTotalPayAmount();
        salerYfzkOutVo.setTotalPayAmount(totalPayAmount);

        BigDecimal needPayAmount = totalGhAmount.subtract(totalPayAmount);
        salerYfzkOutVo.setNeedPayAmount(needPayAmount);

        /**
         * 第三方供应商余额
         */
        // 余额查询
        String msg = FuSiLingApiService.balanceApi();
        log.info(msg);
        // 解析数据
        BalanceJsonData jsonData = JSON.parseObject(msg, BalanceJsonData.class);
        if (jsonData.getData() != null) {
            BigDecimal moneyCz = new BigDecimal(jsonData.getData().getMoneyCz());
            salerYfzkOutVo.setBalance(moneyCz);
        }

        // 电影累计供货金额
        BigDecimal flimCount = map == null ? BigDecimal.ZERO : mapCount.getOrDefault(1, BigDecimal.ZERO);
        // 蛋糕累计供货金额
        BigDecimal cakeCount = map == null ? BigDecimal.ZERO : mapCount.getOrDefault(2, BigDecimal.ZERO);
        // 电子券累计供货金额
        BigDecimal dzqCount = map == null ? BigDecimal.ZERO : mapCount.getOrDefault(3, BigDecimal.ZERO);
        // 电商累计供货金额
        BigDecimal dsCount = map == null ? BigDecimal.ZERO : mapCount.getOrDefault(4, BigDecimal.ZERO);
        // 累计供货金额
        BigDecimal totalCount = flimCount.add(cakeCount).add(dzqCount).add(dsCount);

        salerYfzkOutVo.setFlimCount(flimCount);
        salerYfzkOutVo.setCakeCount(cakeCount);
        salerYfzkOutVo.setDzqCount(dzqCount);
        salerYfzkOutVo.setDsCount(dsCount);
        salerYfzkOutVo.setTotalCount(totalCount);
        return salerYfzkOutVo;
    }
}
